# """
# 将一个给定字符串 s 根据给定的行数 numRows ，以从上往下、从左到右进行 Z 字形排列。
#
# 比如输入字符串为 "PAYPALISHIRING" 行数为 3 时，排列如下：
#
# P   A   H   N
# A P L S I I G
# Y   I   R
# 之后，你的输出需要从左往右逐行读取，产生出一个新的字符串，比如："PAHNAPLSIIGYIR"。
#
# 请你实现这个将字符串进行指定行数变换的函数：
#
# string convert(string s, int numRows);
#
# """
# class Solution:
#     def convert(self, s: str, numRows: int) -> str:
#         if numRows == 1 or numRows >= len(s):
#             return s
#         s2 = self.process_string(s, numRows)
#         l1 = list(s2)
#         l2 = []
#         for i in range(0, numRows):
#             for index, n in enumerate(l1):
#                 if (index % numRows == i):
#                     l2.append(n)
#         result = [item.replace(' ', '') for item in l2]
#         return ''.join(result)
#     def process_string(self, s, n):
#         result = ''
#         group_length = 2 * n - 2
#         for start in range(0, len(s), group_length):
#             end = min(start + group_length, len(s))
#             group = s[start:end]
#             for i in range(len(group)):
#                 char_index = start + i
#                 if n <= i < 2 * n - 2:
#                     left_spaces = 2 * n - (char_index - start + 1) - 1
#                     right_spaces = (char_index - start + 1) - n
#                     result += ' ' * left_spaces + group[i] + ' ' * right_spaces
#                 else:
#                     result += group[i]
#         return result


# 我的思路是 将str转换为标准的字符串 然后再根据行数进行切割

class Solution:
    def convert(self, s: str, numRows: int) -> str:
        if numRows == 1 or numRows >= len(s):
            return s

        L = [''] * numRows
        index, step = 0, 1

        for char in s:
            L[index] += char
            if index == 0:
                step = 1
            elif index == numRows - 1:
                step = -1
            index += step

        return ''.join(L)


num = Solution()

print(num.convert("PAYPALISHIRING", 5))
